package eval.vm;

import com.google.inject.Inject;

/**
 * Create VMs
 * 
 * @author Amit Peled   <amit.kapina@gmail.com>
 * @author Igal Kaufman <igal.kaufman@gmail.com>
 * */
public class VMFactory implements IVMFactory {

	private IVMManager vmManager;

	private int idCounter = 0;

	/**
	 * @Constructor
	 * @param vmManager
	 */
	@Inject
	public VMFactory(IVMManager vmManager) {
		this.vmManager = vmManager;
	}

	/* (non-Javadoc)
	 * @see eval.vm.IVMFactory#createNewVirtualMachine(int)
	 */
	public VirtualMachine createNewVirtualMachine(int lifeTime) {
		VMConfigurationEnum configurationEnum = vmManager
				.generateNewVmAccordingToDistribution();
		return createNewVirtualMachine(configurationEnum,lifeTime);
	}

	/* (non-Javadoc)
	 * @see eval.vm.IVMFactory#createNewVirtualMachine(eval.vm.VMConfigurationEnum, int)
	 */
	@Override
	public VirtualMachine createNewVirtualMachine(VMConfigurationEnum vmType,int lifeTime) {
		VMConfiguration vmConfiguration = vmManager.getVMConfigurationForEnum(vmType);
		VirtualMachine vm = new VirtualMachine(idCounter++, vmType,vmConfiguration,lifeTime);
		vmManager.addKnownVM(vm);
		return vm;

	}
}
